package com.yanggu.mapreduce.sort;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2020/2/7 13:46
 * @description:
 */
public class FlowCountSortMapper extends Mapper<LongWritable, Text, FlowBean, Text> {

    private FlowBean k = new FlowBean();

    private Text v = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //13509468723	7335	110349	117684
        //1. 获取一行数据
        String line = value.toString();

        //2. 按照\t切割
        String[] fields = line.split("\t");

        //3. 封装map输出的key和value
        //手机号码
        String phone = fields[0];
        //上行流量
        long upFlow = Long.parseLong(fields[1]);
        //下行流量
        long downFlow = Long.parseLong(fields[2]);
        //总流量
        long sumFlow = Long.parseLong(fields[3]);
        //封装key
        k.setSumFlow(sumFlow);
        k.setUpFlow(upFlow);
        k.setDownFlow(downFlow);
        //封装value
        v.set(phone);

        //4. 写出
        context.write(k, v);
    }
}
